package cate.game.play.skill.effect;


import cate.common.table.d.GD;
import cate.common.table.d.GDBuff;
import cate.common.table.d.GDSkill;
import cate.common.util.SplitParser;
import cate.common.util.XT;
import cate.game.play.buff.BuffParam;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.config.buff.ExBuffCfg;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.skill.effect.base.SE;
import cate.game.play.util.FighterFilter;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.List;

@NoteClass("buff添加技能效果")
public class BuffAddSE extends SE {

	@NoteField("参数")
	private PlayCfgArgs args;

	@NoteField("buffId")
	public int buffId;

	@NoteField("概率")
	public int chance;

	@NoteField("数量")
	public int num;

	@NoteField("顺序")
	public byte order;

	@NoteField("无视抗控")
	private Boolean ignAntiCtrl;

	@NoteField("附加buff")
	private List<ExBuffCfg> exBuffs;


	//id=1007101&概率=10000&数量=1
	//&顺序=2&无视抗控=1
	//&附加buff=2:123:10000
	@Override
	public void parse(String code) {
		this.args = new PlayCfgArgs(code);
		this.buffId = args.getInt("id", 0);
		this.chance = args.getInt("概率", GD.W_10000);
		this.num = args.getInt("数量", 1);
		this.order = args.getByte("顺序", GDBuff.SettleOrder.AFTER);
		this.ignAntiCtrl = args.getBoolean("无视抗控", null);

		this.exBuffs = SplitParser.parse(ExBuffCfg.class, args.get("附加buff"));
	}

	@Override
	public void settle(SkillActionCtx action, Fighter target) {
		if (FighterFilter.test(args, action.getActor(), target)) {
			AttrPair ap = AttrPair.calc(this, action, target);
			//buff参数
			BuffParam param = null;
			if (ignAntiCtrl != null) {
				param = new BuffParam().ignAntiCtrlSet(ignAntiCtrl);
			}
			List<Integer> addBuffs = target.buff.tryAddSome(action, action.getActor(), ap.actor, ap.target, buffId, num, getChance(target), param);
			if (XT.isNotBlank(addBuffs)) {
				handleExBuff(action, target);
			}
		}
	}

	private int getChance(Fighter target) {
		return chance + parent.getExBuffChance(target.getPid());
	}

	private void handleExBuff(SkillActionCtx action, Fighter target) {
		if (XT.isBlank(exBuffs)) {
			return;
		}
		for (ExBuffCfg cfg : exBuffs) {
			switch (cfg.base) {
				case GDSkill.Base.CASTER:
					action.getActor().buff.tryAddByTid(action, action.getActor(), cfg.buff, cfg.chance);
					break;
				case GDSkill.Base.TARGET:
					target.buff.tryAddByTid(action, action.getActor(), cfg.buff, cfg.chance);
					break;
				default:
					break;
			}
		}
	}
}
